3
3
.
.
3
3
.
.
7
7
G
G
e
e
o
o
m
m
e
e
t
t
r
r
y
y
R
R
e
e
a
a
d
d
e
e
r
r
I
I
n
n
f
f
o
o
[
[
R
R
]
]
Geometry Reader
returns View which is returned by Trailing Closure
returns View which takes up all the space allowed by the Parent
allows us to determine the size of Parent geo.size.width
allows us to read the size that was proposed by the parent (then use that to manipulate our view)
Coordinate spaces
global space (measuring view’s frame relative to the screen)
local space (measuring view’s frame relative to the parent)
custom space (measuring view’s frame relative to the selected View) .coordinateSpace(name: "Custom")
S
S
i
i
z
z
e
e
o
o
f
f
t
t
h
h
e
e
P
P
a
a
r
r
e
e
n
n
t
t
In this example we use geo.size.width to get the width allowed by the Parent.
Green border demonstrates that View returned by GeometryReader takes up all the space allowed by the Parent.
ContentView.swift
struct ContentView: View {
var body: some View {
GeometryReader { geo in
Text("Hello, World!")
.frame(width: geo.size.width * 0.9, height: geo.size.height: * 0.5)
.background(Color.red)
}.border(Color.green, width: 5)
}
}
Output
C
C
o
o
o
o
r
r
d
d
i
i
n
n
a
a
t
t
e
e
s
s
p
p
a
a
c
c
e
e
s
s
This example shows how to read View coordinates in different Coordinate spaces.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Top")
.frame(width: 200, height: 200)
.background(Color.blue)
.coordinateSpace(name: "Custom")
GeometryReader { geo in
Text("Hello, World!")
.frame(width: geo.size.width * 0.9, height: geo.size.height * 0.5)
.background(Color.red)
.onTapGesture {
let global = geo.frame(in: .global)
let local = geo.frame(in: .local)
let custom = geo.frame(in: .named("Custom"))
print("GLOBAL ----")
print("origin = \(global.origin)")
print("min = \(global.minX), \(global.minY)")
print("mid = \(global.midY), \(global.midY)")
print("max = \(global.maxX), \(global.maxY)")
print("size = \(global.size)")
print("wh = \(global.width), \(global.height)")
print("LOCAL ----")
print("origin = \(local.origin)")
print("min = \(local.minX), \(local.minY)")
print("mid = \(local.midY), \(local.midY)")
print("max = \(local.maxX), \(local.maxY)")
print("size = \(local.size)")
print("wh = \(local.width), \(local.height)")
print("CUSTOM ----")
print("origin = \(custom.origin)")
print("min = \(custom.minX), \(custom.minY)")
print("mid = \(custom.midY), \(custom.midY)")
print("max = \(custom.maxX), \(custom.maxY)")
print("size = \(custom.size)")
print("wh = \(custom.width), \(custom.height)")
}
}.border(Color.green, width: 5)
}
}
}
Coordinate spaces